<template>
  <el-container>
    <el-header>
      Header
      <!-- 退出之后,不能访问后台的页面了,跳转到首页|登录页  -->
      <el-button @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 不写的属性,用默认值,写了的和默认值相反 width覆盖掉默认的宽  -->
      <el-aside width=''>
        <!-- <div @click="isCollapse = !isCollapse" >|||</div> -->
        <div @click="chgCollapse">|||</div>
        <el-menu class="el-menu-vertical-demo" background-color="#545c64"
                 text-color="#fff" :default-active='activepath'
                 active-text-color="#0078D7" :collapse='isCollapse'
                 :collapse-transition='false' unique-opened router>
          <!-- menu-item是submenu的选项 index没有开启router index唯一性的代表 一个路由,index 必须是字符串,item.id给的是数字 -->
          <el-submenu :index="item.id+''" v-for="item in menuList"
                      :key="item.id">
            <template slot="title">
              <!-- 阿里的字体图标库 -->
              <i :class="iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 必须加/ 表示从项目的根路径开始,变成绝对路径 -->
            <el-menu-item :index="'/'+subitem.path"
                          v-for="subitem in item.children" :key="subitem.id">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subitem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <!-- 需要给出路由的占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data: function () {
    return {
      isCollapse: false,
      menuList: [],
      /* 图标和id对应关系的类 */
      iconObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      }
    }
  },
  // 需要页面加载的时候,就可以访问到,并展示数据
  mounted () {
    // 获取到列表信息
    this.getMenuList()
  },
  computed: {
    activepath: function () {
      console.log(this.$route.path)
      return this.$route.path
    }
  },
  methods: {
    // es6方法的简写
    logout () {
      /* 清空token,将token制成null,sessionStorage登录,或者是访问者浏览器才可以用 */
      window.sessionStorage.clear();
      // 跳转首页|登录页
      this.$router.push('/')
      // window.sessionStorage.setItem('token',null);
    },
    // logout:function () {    }
    /* 获取当前的菜单列表async await返回的值必须是promise才可以的  */
    async getMenuList () {
      // es6的解构
      const { data: { data, meta } } = await this.$axios.get("menus")
      if (meta.status !== 200) {
        this.$message.error('获取左侧列表失败了')
        return
      }
      this.menuList = data
      // console.log(this.menuList)
    },
    chgCollapse () {
      this.isCollapse = !this.isCollapse
    }
  }
}
</script>

<style lang="css" scoped>
.el-container {
  height: 100%;
}
.el-header {
  background-color: #b3c0d1;
}
.el-aside {
  background-color: #d3dce6;
}
.el-main {
  background-color: #e9eef3;
}
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 150px;
  min-height: 400px;
}
</style>